import { aeRawWave2List, getAEFFTWaveX, getAeWaveX, getAeWaveY } from "@/utils/appCommon/waveMath/aeWaveMath";
import { getViFFTWaveX, getViWaveX, viRawWave2List } from "@/utils/appCommon/waveMath/viWaveMath";
import { fftTransform, waveFFTSupply } from "@/utils/appCommon/fftUtil";

// 通用的获取波形展示数据
//productKey 产品key
// waveType 波形类型 ae  vi
// waveHex 原始的波形hex  目前是时域的
// paraData 算波形时 额外需要的参数
// targetDomain  0时域 1频域
//@ts-ignore
export async function getCommonMathWave(productKey:string, waveType:string, waveHex:string, paraData:any, targetDomain:any) {
  let result = {
     xdata: [],
     xunit: "",
     ydata: [],
     yunit: "dB",
  }
  try{
    if(waveType == "ae"){
      let parseWaveList :Array<number>= aeRawWave2List(waveHex)
      let yres = getAeWaveY(productKey,parseWaveList, paraData.aeGainValue,paraData.aeEnlargeValue)
      let xres = getAeWaveX(productKey,parseWaveList, paraData.aeMeasureSpeed)
      if(targetDomain == 1){
        // 必须得是2的指数，需要处理一下
        parseWaveList = waveFFTSupply(parseWaveList)
        let ydata = fftTransform(parseWaveList,false)
        //@ts-ignore
        yres = {
          list: ydata,
          unit: "dB"
        }
        xres = getAEFFTWaveX(ydata, paraData.aeMeasureSpeed )
      }
      result.xdata = xres.list
      result.xunit = xres.unit
      result.ydata = yres.list
      result.yunit = yres.unit
    }else if(waveType == "vi"){
      let parseWaveList :Array<number>= viRawWave2List(waveHex)
      let yres = {
        list: parseWaveList,
        unit: "dB",
      }
      let xres = getViWaveX(parseWaveList, paraData.viMeasureFreq )
      if(targetDomain == 1){
        let ydata = fftTransform(parseWaveList,false)
        yres = {
          list: ydata,
          unit: "dB"
        }
        xres = getViFFTWaveX(ydata,  paraData.viMeasureFreq  )
      }
      result.xdata = xres.list
      result.xunit = xres.unit
      result.ydata = yres.list
      result.yunit = yres.unit
    }
  }catch(error){
    console.error(error)
  }
  return Promise.resolve(result)
}
